package com.taxigang.server;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.allen_sauer.gwt.log.client.Log;
import com.google.appengine.api.datastore.Key;
import com.taxigang.shared.LoginInfo;
import com.taxigang.shared.TaxiGangInfo;
import com.taxigang.shared.WishRouteInfo;

public class WishRouteDAO {
	private EntityManager em;
	public WishRouteDAO(EntityManager em){
		this.em = em;
	}
	synchronized public boolean add(TaxiGangInfo taxigangInfo, LoginInfo loginInfo) {
		WishRoute wishRoute = new WishRoute(taxigangInfo,loginInfo);
		try {
			em.getTransaction().begin();
			em.persist(wishRoute);
			em.getTransaction().commit();
		} catch (Exception ex) {
			Log.error(ex.getMessage());
		} finally {
			// em.close();
			return false;
		}
	}

	synchronized public boolean delete(String wishRouteId) {
		try {
			long id = Long.parseLong(wishRouteId);
			
			WishRoute wishRoute = em.find(WishRoute.class, id);
			em.getTransaction().begin();
			em.remove(wishRoute);
			em.getTransaction().commit();
		} catch (Exception ex) {
			Log.error(ex.getMessage());
		} finally {
			// em.close();
			return false;
		}
	}


	public Collection<WishRouteInfo> find(TaxiGangInfo taxiganginfo) {

		Map<Long,WishRouteInfo> wishRouteList = new HashMap<Long,WishRouteInfo>();
		collect("WishRoute.findByStartProximity",taxiganginfo.getStartLat(), taxiganginfo.getStartLng(),wishRouteList);
		collect("WishRoute.findByStartProximity",taxiganginfo.getDestinationLat(), taxiganginfo.getDestinationLng(),wishRouteList);
		collect("WishRoute.findByDestProximity",taxiganginfo.getStartLat(), taxiganginfo.getStartLng(),wishRouteList);
		collect("WishRoute.findByDestProximity",taxiganginfo.getDestinationLat(), taxiganginfo.getDestinationLng(),wishRouteList);

		return wishRouteList.values();
	}

	private void collect(String queryStr, double lat, double lng, Map<Long,WishRouteInfo> wishRouteList){
		Query query = em.createNamedQuery(queryStr);
		long[][] geoboxs = GeoBox.getInstance().getBox(
				lat, lng);

		query.setParameter("geobox00", geoboxs[0][0]);
		query.setParameter("geobox01", geoboxs[0][1]);
		query.setParameter("geobox02", geoboxs[0][2]);
		query.setParameter("geobox10", geoboxs[1][0]);
		query.setParameter("geobox11", geoboxs[1][1]);
		query.setParameter("geobox12", geoboxs[1][2]);
		query.setParameter("geobox20", geoboxs[2][0]);
		query.setParameter("geobox21", geoboxs[2][1]);
		query.setParameter("geobox22", geoboxs[2][2]);

		try {
			Collection<WishRoute> wishRoutes = query.getResultList();
			Log.debug("TaxiGangDAO:getTaxiGangsByStartProximity found "
					+ wishRoutes.size() + " matches");

			for (WishRoute wishRoute : wishRoutes) {
				WishRouteInfo wishRouteInfo = wishRoute.getInfo();
				wishRouteList.put(wishRouteInfo.getId(),wishRouteInfo);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}


}
